using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SolarWinds.Helpers;
using SolarWinds.Input;

namespace SolarWinds.Widgets
{
    public class PushButton : Button
    {
        enum ColorStates
        {
            Hover,
            Focus,
            Pressed,
            Released
        }

        private ColorStates mState = ColorStates.Released;
        private bool mStateChangedFlag;

        public Menu Menu { get; set; }

        public PushButton(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        protected internal override void PaintEvent()
        {
            base.PaintEvent();

            // strip string
            String stripCaption = SpriteFontHelper.StripString(Font, Caption, Width - 2 * mTileSize.X);

            Vector2 fontSize = Font.MeasureString(stripCaption);

            var pos = new Vector2(ScreenPosition.X + (Width - fontSize.X) / 2,
                                  ScreenPosition.Y + (Height - fontSize.Y) / 2);

            SpriteBatch.DrawString(Font, stripCaption, pos, TextColor);
        }

        // TODO move to base class
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (mStateChangedFlag)
            {
                switch (mState)
                {
                    case ColorStates.Hover:
                        mTileColors[0] = Color.CadetBlue;
                        mTileColors[1] = Color.CadetBlue;
                        mTileColors[2] = Color.CadetBlue;
                        break;
                    case ColorStates.Focus:
                        mTileColors[0] = Color.CornflowerBlue;
                        mTileColors[1] = Color.CornflowerBlue;
                        mTileColors[2] = Color.CornflowerBlue;
                        break;
                    case ColorStates.Pressed:
                        mTileColors[0] = Color.Gray;
                        mTileColors[1] = Color.Gray;
                        mTileColors[2] = Color.Gray;
                        break;
                    case ColorStates.Released:
                        mTileColors[0] = Color.White;
                        mTileColors[1] = Color.White;
                        mTileColors[2] = Color.White;
                        break;
                    default:
                        break;
                }


                mStateChangedFlag = false;
            }
        }

        protected override void InitializeRects()
        {
            mSrcRects = new Rectangle[3];
            mDestRects = new Rectangle[3];
            mTileColors = new Color[3] { Color.White, Color.White, Color.White };

            MinWidth = 2 * mTileSize.X;
            MinHeight = mTileSize.Y;
        }

        protected override void UpdateSrcDestRects()
        {
            // left
            mSrcRects[0] = TileIndex2Rectangle(new Point(4, 1));
            mDestRects[0] = new Rectangle(ScreenPosition.X, ScreenPosition.Y, mTileSize.X, Height);
            // middle
            mSrcRects[1] = TileIndex2Rectangle(new Point(5, 1));
            mDestRects[1] = new Rectangle(ScreenPosition.X + mTileSize.X, ScreenPosition.Y, Width - 2 * mTileSize.X, Height);
            // right
            mSrcRects[2] = TileIndex2Rectangle(new Point(6, 1));
            mDestRects[2] = new Rectangle(ScreenPosition.X - mTileSize.X + Width, ScreenPosition.Y, mTileSize.X, Height);
        }

        protected internal override void MousePressEvent(MouseEvent evt)
        {
            mState = ColorStates.Pressed;
            mStateChangedFlag = true;

            base.MousePressEvent(evt);
        }

        protected internal override void MouseReleaseEvent(MouseEvent evt)
        {
            if (IsMouseOver)
            {
                mState = ColorStates.Hover;
            }
            else if (HasKeyFocus)
            {
                mState = ColorStates.Focus;
            }
            else
            {
                mState = ColorStates.Released;                
            }

            mStateChangedFlag = true;

            base.MouseReleaseEvent(evt);
        }

        protected internal override void MouseOverEvent(MouseEvent evt)
        {
            mState = ColorStates.Hover;
            mStateChangedFlag = true;
        }

        protected internal override void MouseOutEvent(MouseEvent evt)
        {
            mState = (HasKeyFocus) ? ColorStates.Focus : ColorStates.Released;
            mStateChangedFlag = true;
        }

        protected internal override void KeyFocusInEvent(KeyEvent evt)
        {
            mState = ColorStates.Focus;
            mStateChangedFlag = true;
        }

        protected internal override void KeyFocusOutEvent(KeyEvent evt)
        {
            mState = (IsMouseOver) ? ColorStates.Hover : ColorStates.Released;
            mStateChangedFlag = true;
        }
    }
}